﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using DiracTCC.Business;
using System.Threading;


namespace DiracTCC.WinForm
{
    public partial class DiracCodecSettings : Form
    {
        private Form1 callerForm;
        private OutputForm outputForm;
        private bool stopPostOutputInfo = false;

        string codificacao, output, input, verbose, local, use_vlc, no_spartition, multi_quants,
            lossless, qf, fr, mv_prec, width, height, cformat, source_sampling, L1_sep, num_L1,
            xblen, yblen, xbsep, ybsep, cpd, wlt_depth, rwlt_filter, iwlt_filter, prefilter,
             full_search, combined_me, field_coding, start, stop, targetrate;
        
        public DiracCodecSettings()
        {
            InitializeComponent();

            // load preset combobox
            DataTable xmlConfig = GetDataTableSchema();
            PreSetsComboBox.DataSource = xmlConfig;
            PreSetsComboBox.DisplayMember = "Name";

            DataTable xmlConfigUser = GetDataTableSchemaUser();
            UserPresetsBox.DataSource = xmlConfigUser;
            UserPresetsBox.DisplayMember = "Name";

        }

        
        private void LimpaTextBoxes(Control c)
        {
            foreach (Control ctr in c.Controls)
            {
                if (ctr.HasChildren)
                {
                    LimpaTextBoxes(ctr);
                }

                if (ctr is TextBox)
                {
                    if (ctr.Name != "projectNameTextBox")
                    {
                        TextBox t = (TextBox)ctr;
                        t.Text = string.Empty;
                    }
                }
            }

        }

        private void LimpaCheckBoxes(Control c)
        {
            foreach (Control ctr in c.Controls)
            {
                if (ctr.HasChildren)
                {
                    LimpaCheckBoxes(ctr);
                }

                if (ctr is CheckBox)
                {
                    CheckBox t = (CheckBox)ctr;
                    t.Checked = false;
                }
            }

        }

        private void LimpaComboBoxes(Control c)
        {
            foreach (Control ctr in c.Controls)
            {
                if (ctr.HasChildren)
                {
                    LimpaComboBoxes(ctr);
                }

                if (ctr is ComboBox)
                {
                    ComboBox t = (ComboBox)ctr;
                    t.Text = null;
                }
            }

        }


        private void EnableStartCodec()
        {
            button1.Enabled = !string.IsNullOrEmpty(projectNameTextBox.Text) &&
                                PreSetsComboBox.SelectedIndex > 0 &&
                                InputLabel.Text != "arquivo de entrada";
        }


                       
        private void button1_Click(object sender, EventArgs e)
        {
            // generate output value!!!
            var cleanInput = input.Replace("\"", string.Empty);
            output = DiracReaderHelper.GetOutputPath(cleanInput, projectNameTextBox.Text);
            output = " \"" + output + "\"";

            // unenable all groups..
            groupBox2.Enabled = false;
            groupBox1.Enabled = false;
            groupBox3.Enabled = false;
            groupBox5.Enabled = false;

            // show and configure progress bar
            var chroma = DiracReaderHelper.GetChromaFormat(cformat.Substring(10));
            YuvAnalyser yuv = new YuvAnalyser(cleanInput, int.Parse(width.Substring(8)), int.Parse(height.Substring(9)), chroma);

            toolStripStatusLabel1.Text = "Processando...";
            toolStripProgressBar1.Visible = true;
            toolStripProgressBar1.Minimum = 1;
            toolStripProgressBar1.Maximum = (int)yuv.GetNumberOfFrames();

            if (verboseBox.Checked)
            {
                outputForm = new OutputForm();
                outputForm.Show();
            }

            // start real job!
            using (var pgr = new Process())
            {
                pgr.StartInfo.CreateNoWindow = true;
                pgr.OutputDataReceived += new DataReceivedEventHandler(pgr_OutputDataReceived);
                pgr.StartInfo.RedirectStandardOutput = true;
                pgr.StartInfo.UseShellExecute = false;

                pgr.StartInfo.FileName = "Helpers\\encodedirac.exe";
                pgr.StartInfo.Arguments = codificacao + verbose + local + use_vlc + no_spartition + multi_quants +
                    lossless + qf + fr + mv_prec + width + height + cformat + source_sampling +
                    L1_sep + num_L1 + xblen + yblen + xbsep + ybsep + cpd + wlt_depth + rwlt_filter + iwlt_filter + prefilter +
                    full_search + combined_me + field_coding + start + stop + targetrate + input + output;

                pgr.Start();
                pgr.BeginOutputReadLine();
            }
        }

        void pgr_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (verboseBox.Checked && !stopPostOutputInfo)
            {
                CallAddText t = new CallAddText(AddTextToOutput);
                this.Invoke(t, e.Data);
            }

            if (e.Data != null && e.Data.Contains("Compressing frame "))
            {
                CallOriginalForm d = new CallOriginalForm(SetControlPropertyValue);
                this.Invoke(d);
            }

            if (e.Data != null && e.Data.Contains("Processing finished!"))
            {
                Thread.Sleep(1000);
                stopPostOutputInfo = true;
                CallOriginalForm d = new CallOriginalForm(FinishProcessing);
                this.Invoke(d);
            }
        }
        
        // stuff to refresh progress bar!
        public delegate void CallAddText(string text);
        public void AddTextToOutput(string text)
        {
            outputForm.AddNewContent(text);
        }
        public delegate void CallOriginalForm();
        public void SetControlPropertyValue()
        {
            if (toolStripProgressBar1.Value + 1 < toolStripProgressBar1.Maximum)
                toolStripProgressBar1.Value += 1;
        }
        public void FinishProcessing()
        {
            // copy original file to the folder!!
            toolStripStatusLabel1.Text = "Copiando filme original para a pasta do projeto...";
            
            var copyFrom = input.Replace("\"", string.Empty);
            var copyTo = Path.ChangeExtension(output.Replace("\"", string.Empty).Trim(), "yuv");
            File.Copy(copyFrom, copyTo);

            // send informations to caller form e start processing!
            callerForm.ShowResults(Path.ChangeExtension(copyTo, "xml"));

            // close this form
            this.Close();
            this.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {            
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Video Files|*.yuv";
            openFileDialog1.Title = "Select a Video File";
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                input = " " + '"' + openFileDialog1.FileName + '"';
                InputLabel.Text = input;

                EnableStartCodec();
            }

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (verboseBox.Checked == true)
                verbose = " -verbose";
            else
                verbose = "";

        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (use_vlcBox.Checked == true)
                use_vlc = " -use_vlc";
            else
                use_vlc = "";
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (localBox.Checked == true)
                local = " -local";            
            else
                local = "";
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (no_spartitionBox.Checked == true)
                no_spartition = " -no_spartition";
            else
                no_spartition = "";
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (multi_quantsBox.Checked == true)
                multi_quants = " -multi_quants";
            else
                multi_quants = "";
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (losslessBox.Checked == true)
                lossless = " -lossless";
            else
                lossless = "";
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            // Create new SaveFileDialog object
            SaveFileDialog DialogSave = new SaveFileDialog();

            // Default file extension
            DialogSave.DefaultExt = "drc";

            // Available file extensions
            DialogSave.Filter = "Dirac file (*.drc)|*.drc|All files (*.*)|*.*";

            // Adds a extension if the user does not
            DialogSave.AddExtension = true;

            // Restores the selected directory, next time
            DialogSave.RestoreDirectory = true;

            // Dialog title
            DialogSave.Title = "Where do you want to save the file?";

            // Startup directory
            DialogSave.InitialDirectory = @"C:/";

            // Show the dialog and process the result
            if (DialogSave.ShowDialog() == DialogResult.OK)
            {                
                output = " " + '"' + DialogSave.FileName.ToString() + '"';
                projectNameTextBox.Text = output;
                
            }
            else
            {
                MessageBox.Show("You hit cancel or closed the dialog.");
            }

            DialogSave.Dispose();
            DialogSave = null;
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void qfBox_CheckedChanged(object sender, EventArgs e)
        {
            if (qfBox1.Text == "")
                qfBox.Checked = false;

            if (qfBox.Checked == true)
                qf = " -qf " + qfBox1.Text.ToString();
            else
                qf = ""; 
        }

        private void qfBox1_TextChanged(object sender, EventArgs e)
        {
            if (qfBox1.Text != "")
                qfBox.Checked = true;
            else
                qfBox.Checked = false;

            if (qfBox.Checked == true)
                qf = " -qf " + qfBox1.Text.ToString();
            
        }

        private void frBox_CheckedChanged(object sender, EventArgs e)
        {
            if (frBox1.Text == "")
                frBox.Checked = false;

            if (frBox.Checked == true)
                fr = " -fr " + frBox1.Text.ToString();
            else
                fr = ""; 
        }

        private void frBox1_TextChanged(object sender, EventArgs e)
        {
            if (frBox1.Text != "")
                frBox.Checked = true;
            else
                frBox.Checked = false;
            if (frBox.Checked == true)
                fr = " -fr " + frBox1.Text.ToString();
        }

        private void mv_precBox_CheckedChanged(object sender, EventArgs e)
        {
            if (mv_precBox1.SelectedItem == null)
                mv_precBox.Checked = false;

            if (mv_precBox.Checked == true)
                mv_prec = " -mv_prec " + mv_precBox1.SelectedItem.ToString();
            else
                mv_prec = ""; 
        }

        private void mv_precBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mv_precBox1.SelectedIndex > 0)
                mv_precBox.Checked = true;
            else
                mv_precBox.Checked = false;

            if (mv_precBox.Checked == true)
                mv_prec = " -mv_prec " + mv_precBox1.SelectedItem.ToString();
            mv_precBox.Checked = true;
        }

        private void XmlButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Quer realmente salvar " + '"' + UserPresetsBox.Text + '"' + "?", "Salvar", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                DataTable xmlConfigUser = GetDataTableSchemaUser();

                //xmlConfig.Rows.Add("SDTV", 3,32, 1);
                //xmlConfig.Rows.Add("CIF", 23, 99, 0.125);
                //xmlConfig.Rows.Add("HDTV", 1, 1, 1);


                //inicio if "se existir preset "name" faça isso"

                var row = (from r in xmlConfigUser.Select()
                           where r["Name"].ToString() == UserPresetsBox.Text
                           select r).FirstOrDefault();


                if (row != null)
                    xmlConfigUser.Rows.Remove(row);
                // fim do if

                xmlConfigUser.Rows.Add(UserPresetsBox.Text, input, output, PreSetsComboBox.Text, widthBox.Checked, widthBox1.Text, heightBox.Checked,
                    heightBox1.Text, cformatBox.Checked, cformatBox1.Text, frBox.Checked, frBox1.Text, source_samplingBox.Checked,
                    source_samplingBox1.Text, use_vlcBox.Checked, no_spartitionBox.Checked, multi_quantsBox.Checked, losslessBox.Checked,
                    qfBox.Checked, qfBox1.Text, mv_precBox.Checked, mv_precBox1.Text, L1_sepBox.Checked, L1_sepBox1.Text, num_L1Box.Checked,
                    num_L1Box1.Text, xblenBox.Checked, xblenBox1.Text, yblenBox.Checked, yblenBox1.Text, xbsepBox.Checked, xbsepBox1.Text,
                    ybsepBox.Checked, ybsepBox1.Text, cpdBox.Checked, cpdBox1.Text, rwlt_filterBox.Checked, rwlt_filterBox1.Text,
                    wlt_depthBox.Checked, wlt_depthBox1.Text, iwlt_filterBox.Checked, iwlt_filterBox1.Text, prefilterBox.Checked,
                    prefilterBox1.Text, full_searchBox.Checked, combined_meBox.Checked, field_codingBox.Checked, verboseBox.Checked,
                    localBox.Checked, startBox.Checked, startBox1.Text, stopBox.Checked, stopBox1.Text, targetrateBox.Checked, targetrateBox1.Text);

                xmlConfigUser.WriteXml("presets_user.xml", XmlWriteMode.WriteSchema);

                string last = UserPresetsBox.Text;
                UserPresetsBox.DataSource = xmlConfigUser;
                UserPresetsBox.DisplayMember = "Name";
                UserPresetsBox.Refresh();
                UserPresetsBox.Text = last;
            }
        }

        private void GetValueButton_Click(object sender, EventArgs e)
        {
            
        }

        private static DataTable GetDataTableSchema()
        {
            DataTable xmlConfig = new DataTable("Configurations");
            //xmlConfig.Columns.Add("Name", typeof(string));
            //xmlConfig.Columns.Add("qf", typeof(string));
            //xmlConfig.Columns.Add("fr", typeof(string));
            //xmlConfig.Columns.Add("mv_prec", typeof(string));

            xmlConfig.ReadXml("presets.xml");

            return xmlConfig;
        }

        private static DataTable GetDataTableSchemaUser()
        {
            DataTable xmlConfigUser = new DataTable("Configurations");
            //xmlConfig.Columns.Add("Name", typeof(string));
            //xmlConfig.Columns.Add("qf", typeof(string));
            //xmlConfig.Columns.Add("fr", typeof(string));
            //xmlConfig.Columns.Add("mv_prec", typeof(string));

            xmlConfigUser.ReadXml("presets_user.xml");

            return xmlConfigUser;
        }

        private void PreSetsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            EnableStartCodec();

            codificacao = "-" + PreSetsComboBox.Text.ToString();

            LimpaTextBoxes(this);
            LimpaCheckBoxes(this);
            //LimpaComboBoxes(this);

            if (PreSetsComboBox.SelectedIndex > 0)
            {
                DataTable xmlConfig = GetDataTableSchema();

                var row = (from r in xmlConfig.Select()
                           where r["Name"].ToString() == PreSetsComboBox.Text
                           select r).First();

                qfBox1.Text = row["qf"].ToString();
                qfBox.Checked = true;
                frBox1.Text = row["fr"].ToString();
                frBox.Checked = true;
                mv_precBox1.Text = row["mv_prec"].ToString();
                mv_precBox.Checked = true;
                widthBox1.Text = row["width"].ToString();
                widthBox.Checked = true;
                cformatBox1.Text = row["cformat"].ToString();
                cformatBox.Checked = true;
                heightBox1.Text = row["height"].ToString();
                heightBox.Checked = true;
                iwlt_filterBox1.Text = row["iwlt_filter"].ToString();
                iwlt_filterBox.Checked = true;
                rwlt_filterBox1.Text = row["rwlt_filter"].ToString();
                rwlt_filterBox.Checked = true;
            }
        }

        private void toolTip1_Popup(object sender, PopupEventArgs e)
        {

        }



        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void widthBox_CheckedChanged(object sender, EventArgs e)
        {
            if (widthBox1.Text == "")
                widthBox.Checked = false;

            if (widthBox.Checked == true)
                width = " -width " + widthBox1.Text.ToString();
            else
                width = ""; 
        }

        private void widthBox1_TextChanged(object sender, EventArgs e)
        {
            if (widthBox1.Text != "")
                widthBox.Checked = true;
            else
                widthBox.Checked = false;
            if (widthBox.Checked == true)
                width = " -width " + widthBox1.Text.ToString();            
        }

        private void heightBox_CheckedChanged(object sender, EventArgs e)
        {
            if (heightBox1.Text == "")
                heightBox.Checked = false;

            if (heightBox.Checked == true)
                height = " -height " + heightBox1.Text.ToString();
            else
                height = "";
        }

        private void heightBox1_TextChanged(object sender, EventArgs e)
        {
            if (heightBox1.Text != "")
                heightBox.Checked = true;
            else
                heightBox.Checked = false;
            if (heightBox.Checked == true)
                height = " -height " + heightBox1.Text.ToString();
        }

        private void cformatBox_CheckedChanged(object sender, EventArgs e)
        {
            if (cformatBox1.SelectedItem == null)
                cformatBox.Checked = false;

            if (cformatBox.Checked == true)
                cformat = " -cformat " + cformatBox1.SelectedItem.ToString();
            else
                cformat = ""; 
        }

        private void cformatBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cformatBox1.SelectedIndex > 0)
                cformatBox.Checked = true;
            else
                cformatBox.Checked = false;
            if (cformatBox.Checked == true)
                cformat = " -cformat " + cformatBox1.SelectedItem.ToString();
        }

        private void source_samplingBox_CheckedChanged(object sender, EventArgs e)
        {
            if (source_samplingBox1.SelectedItem == null)
                source_samplingBox.Checked = false;

            if (source_samplingBox.Checked == true)
                source_sampling = " -source_sampling " + source_samplingBox1.SelectedItem.ToString();
            else
                source_sampling = ""; 
        }

        private void source_samplingBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (source_samplingBox1.SelectedIndex > 0)
                source_samplingBox.Checked = true;
            else
                source_samplingBox.Checked = false;
            if (source_samplingBox.Checked == true)
                source_sampling = " -source_sampling " + source_samplingBox1.SelectedItem.ToString();
        }
        
        private void L1_sepBox_CheckedChanged(object sender, EventArgs e)
        {
            if (L1_sepBox1.Text == "")
                L1_sepBox.Checked = false;

            if (L1_sepBox.Checked == true)
                L1_sep = " -L1_sep " + L1_sepBox1.Text.ToString();
            else
                L1_sep = "";
        }

        private void L1_sepBox1_TextChanged(object sender, EventArgs e)
        {
            if (L1_sepBox1.Text != "")
                L1_sepBox.Checked = true;
            else
                L1_sepBox.Checked = false;

            if (L1_sepBox.Checked == true)
                L1_sep = " -L1_sep " + L1_sepBox1.Text.ToString();
            L1_sepBox.Checked = true;
        }

        private void num_L1Box_CheckedChanged(object sender, EventArgs e)
        {
            if (num_L1Box1.Text == "")
                num_L1Box.Checked = false;

            if (num_L1Box.Checked == true)
                num_L1 = " -num_L1 " + num_L1Box1.Text.ToString();
            else
                num_L1 = "";
        }

        private void num_L1Box1_TextChanged_1(object sender, EventArgs e)
        {
            if (num_L1Box1.Text != "")
                num_L1Box.Checked = true;
            else
                num_L1Box.Checked = false;

            if (num_L1Box.Checked == true)
                num_L1 = " -num_L1 " + num_L1Box1.Text.ToString();
        }

        private void xblenBox_CheckedChanged(object sender, EventArgs e)
        {
            if (xblenBox1.Text == "")
                xblenBox.Checked = false;

            if (xblenBox.Checked == true)
                xblen = " -xblen " + xblenBox1.Text.ToString();
            else
                xblen = "";
        }

        private void xblenBox1_TextChanged(object sender, EventArgs e)
        {
            if (xblenBox1.Text != "")
                xblenBox.Checked = true;
            else
                xblenBox.Checked = false;

            if (xblenBox.Checked == true)
                xblen = " -xblen " + xblenBox1.Text.ToString();
        }  

        private void yblenBox_CheckedChanged(object sender, EventArgs e)
        {
            if (yblenBox1.Text == "")
                yblenBox.Checked = false;

            if (yblenBox.Checked == true)
                yblen = " -yblen " + yblenBox1.Text.ToString();
            else
                yblen = "";
        }

        private void yblenBox1_TextChanged(object sender, EventArgs e)
        {
            if (yblenBox1.Text != "")
                yblenBox.Checked = true;
            else
                yblenBox.Checked = false;

            if (yblenBox.Checked == true)
                yblen = " -yblen " + yblenBox1.Text.ToString();
        }

        private void xbsepBox_CheckedChanged(object sender, EventArgs e)
        {
            if (xbsepBox1.Text == "")
                xbsepBox.Checked = false;

            if (xbsepBox.Checked == true)
                xbsep = " -xbsep " + xbsepBox1.Text.ToString();
            else
                xbsep = "";
        }

        private void xbsepBox1_TextChanged(object sender, EventArgs e)
        {
            if (xbsepBox1.Text != "")
                xbsepBox.Checked = true;
            else
                xbsepBox.Checked = false;

            if (xbsepBox.Checked == true)
                xbsep = " -xbsep " + xbsepBox1.Text.ToString();
        }

        private void ybsepBox_CheckedChanged(object sender, EventArgs e)
        {
            if (ybsepBox1.Text == "")
                ybsepBox.Checked = false;

            if (ybsepBox.Checked == true)
                ybsep = " -ybsep " + ybsepBox1.Text.ToString();
            else
                ybsep = "";
        }

        private void ybsepBox1_TextChanged(object sender, EventArgs e)
        {
            if (ybsepBox1.Text != "")
                ybsepBox.Checked = true;
            else
                ybsepBox.Checked = false;

            if (ybsepBox.Checked == true)
                ybsep = " -ybsep " + ybsepBox1.Text.ToString();
        }

        private void cpdBox_CheckedChanged(object sender, EventArgs e)
        {
            if (cpdBox1.Text == "")
                cpdBox.Checked = false;

            if (cpdBox.Checked == true)
                cpd = " -cpd " + cpdBox1.Text.ToString();
            else
                cpd = "";
        }

        private void cpdBox1_TextChanged(object sender, EventArgs e)
        {
            if (cpdBox1.Text != "")
                cpdBox.Checked = true;
            else
                cpdBox.Checked = false;

            if (cpdBox.Checked == true)
                cpd = " -cpd " + cpdBox1.Text.ToString();
        }

        private void rwlt_filterBox_CheckedChanged(object sender, EventArgs e)
        {
            if (rwlt_filterBox1.SelectedItem == null)
                rwlt_filterBox.Checked = false;

            if (rwlt_filterBox.Checked == true)
                rwlt_filter = " -rwlt_filter " + rwlt_filterBox1.SelectedItem.ToString();
            else
                rwlt_filter = ""; 
        }

        private void rwlt_filterBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rwlt_filterBox1.SelectedIndex > 0)
                rwlt_filterBox.Checked = true;
            else
                rwlt_filterBox.Checked = false;

            if (rwlt_filterBox.Checked == true)
                rwlt_filter = " -rwlt_filter " + rwlt_filterBox1.SelectedItem.ToString();
        }

        private void wlt_depthBox_CheckedChanged(object sender, EventArgs e)
        {
            if (wlt_depthBox1.Text == "")
                wlt_depthBox.Checked = false;

            if (wlt_depthBox.Checked == true)
                wlt_depth = " -wlt_depth " + wlt_depthBox1.Text.ToString();
            else
                wlt_depth = "";
        }

        private void wlt_depthBox1_TextChanged(object sender, EventArgs e)
        {
            if (wlt_depthBox1.Text != "")
                wlt_depthBox.Checked = true;
            else
                wlt_depthBox.Checked = false;

            if (wlt_depthBox.Checked == true)
               wlt_depth = " -wlt_depth " + wlt_depthBox1.Text.ToString();            
        }

        private void iwlt_filterBox_CheckedChanged(object sender, EventArgs e)
        {
            if (iwlt_filterBox1.SelectedItem == null)
                iwlt_filterBox.Checked = false;

            if (iwlt_filterBox.Checked == true)
                iwlt_filter = " -iwlt_filter " + iwlt_filterBox1.SelectedItem.ToString();
            else
                iwlt_filter = ""; 
        }

        private void iwlt_filterBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iwlt_filterBox1.SelectedIndex > 0)
                iwlt_filterBox.Checked = true;
            else
                iwlt_filterBox.Checked = false;

            if (iwlt_filterBox.Checked == true)
                iwlt_filter = " -iwlt_filter " + iwlt_filterBox1.SelectedItem.ToString();
        }

        private void prefilterBox_CheckedChanged(object sender, EventArgs e)
        {
            if (prefilterBox1.SelectedItem == null)
                prefilterBox.Checked = false;

            if (prefilterBox.Checked == true)
                prefilter = " -prefilter " + prefilterBox1.SelectedItem.ToString() + " " + prefilterBox2.Text;
            else
                prefilter = ""; 
        }

        private void prefilterBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (prefilterBox1.SelectedIndex > 0)
                prefilterBox.Checked = true;
            else
                prefilterBox.Checked = false;

            if (prefilterBox.Checked == true)
                prefilter = " -prefilter " + prefilterBox1.SelectedItem.ToString() + " " + prefilterBox2.Text.ToString();
        }

        private void full_searchBox_CheckedChanged(object sender, EventArgs e)
        {
            if (full_searchBox.Checked == true)
                full_search = " -full_search";
            else
                full_search = "";
        }

        private void combined_meBox_CheckedChanged(object sender, EventArgs e)
        {
            if (combined_meBox.Checked == true)
                combined_me = " -combined_me";
            else
                combined_me = "";
        }

        private void field_codingBox_CheckedChanged(object sender, EventArgs e)
        {
            if (field_codingBox.Checked == true)
                field_coding = " -field_coding";
            else
                field_coding = "";
        }

        private void startBox_CheckedChanged(object sender, EventArgs e)
        {
            if (startBox1.Text == "")
                startBox.Checked = false;

            if (startBox.Checked == true)
                start = " -start " + startBox1.Text.ToString();
            else
                start = "";
        }

        private void startBox1_TextChanged(object sender, EventArgs e)
        {
            if (startBox1.Text != "")
                startBox.Checked = true;
            else
                startBox.Checked = false;

            if (startBox.Checked == true)
                start = " -start " + startBox1.Text.ToString();
        }

        private void stopBox_CheckedChanged(object sender, EventArgs e)
        {
            if (stopBox1.Text == "")
                stopBox.Checked = false;

            if (stopBox.Checked == true)                
                stop = " -stop " + stopBox1.Text.ToString();
            else
                stop = "";
        }

        private void stopBox1_TextChanged(object sender, EventArgs e)
        {
            if (stopBox1.Text != "")
                stopBox.Checked = true;
            else
                stopBox.Checked = false;

            if (stopBox.Checked == true)
                stop = " -stop " + stopBox1.Text.ToString();
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            LimpaTextBoxes(this);
            LimpaCheckBoxes(this);
            LimpaComboBoxes(this);
            output = null;
            input = null;
            InputLabel.Text = "arquivo de entrada";
            
        }

        private void UserPresetsBox_SelectedIndexChanged(object sender, EventArgs e)
        {
         

        }

        private void LoadUserButton_Click(object sender, EventArgs e)
        {
            EnableStartCodec();


            LimpaTextBoxes(this);
            LimpaCheckBoxes(this);
            //LimpaComboBoxes(this);

            DataTable xmlConfigUser = GetDataTableSchemaUser();

            var row = (from r in xmlConfigUser.Select()
                       where r["Name"].ToString() == UserPresetsBox.Text
                       select r).First();
                        
            PreSetsComboBox.Text = row["PreSetsComboBox"].ToString();
            InputLabel.Text = row["input"].ToString();
            input = row["input"].ToString();
            projectNameTextBox.Text = row["name"].ToString();
            

            widthBox1.Text = row["widthBox1"].ToString();
            if (row["widthBox"].ToString() == "True")
                widthBox.Checked = true;
                        
            heightBox1.Text = row["heightBox1"].ToString();
            if (row["heightBox"].ToString() == "True")
                heightBox.Checked = true;

            cformatBox1.Text = row["cformatBox1"].ToString();
            if (row["cformatBox"].ToString() == "True")
                cformatBox.Checked = true;

            frBox1.Text = row["frBox1"].ToString();
            if (row["frBox"].ToString() == "True")
                frBox.Checked = true;

            source_samplingBox1.Text = row["source_samplingBox1"].ToString();
            if (row["source_samplingBox"].ToString() == "True")
                source_samplingBox.Checked = true;
  
            if (row["use_vlcBox"].ToString() == "True")
                use_vlcBox.Checked = true;

            if (row["no_spartitionBox"].ToString() == "True")
                no_spartitionBox.Checked = true;

            if (row["multi_quantsBox"].ToString() == "True")
                multi_quantsBox.Checked = true;

            if (row["losslessBox"].ToString() == "True")
                losslessBox.Checked = true;

            qfBox1.Text = row["qfBox1"].ToString();
            if (row["qfBox"].ToString() == "True")
                qfBox.Checked = true;

            mv_precBox1.Text = row["mv_precBox1"].ToString();
            if (row["mv_precBox"].ToString() == "True")
                mv_precBox.Checked = true;

            L1_sepBox1.Text = row["L1_sepBox1"].ToString();
            if (row["L1_sepBox"].ToString() == "True")
                L1_sepBox.Checked = true;

            num_L1Box1.Text = row["num_L1Box1"].ToString();
            if (row["num_L1Box"].ToString() == "True")
                num_L1Box.Checked = true;

            xblenBox1.Text = row["xblenBox1"].ToString();
            if (row["xblenBox"].ToString() == "True")
                xblenBox.Checked = true;

            yblenBox1.Text = row["yblenBox1"].ToString();
            if (row["yblenBox"].ToString() == "True")
                yblenBox.Checked = true;

            xbsepBox1.Text = row["xbsepBox1"].ToString();
            if (row["xbsepBox"].ToString() == "True")
                xbsepBox.Checked = true;

            ybsepBox1.Text = row["ybsepBox1"].ToString();
            if (row["ybsepBox"].ToString() == "True")
                ybsepBox.Checked = true;

            cpdBox1.Text = row["cpdBox1"].ToString();
            if (row["cpdBox"].ToString() == "True")
                cpdBox.Checked = true;

            rwlt_filterBox1.Text = row["rwlt_filterBox1"].ToString();
            if (row["rwlt_filterBox"].ToString() == "True")
                rwlt_filterBox.Checked = true;

            wlt_depthBox1.Text = row["wlt_depthBox1"].ToString();
            if (row["wlt_depthBox"].ToString() == "True")
                wlt_depthBox.Checked = true;

            iwlt_filterBox1.Text = row["iwlt_filterBox1"].ToString();
            if (row["iwlt_filterBox"].ToString() == "True")
                iwlt_filterBox.Checked = true;

            prefilterBox1.Text = row["prefilterBox1"].ToString();
            if (row["prefilterBox"].ToString() == "True")
                prefilterBox.Checked = true;
            
            if (row["full_searchBox"].ToString() == "True")
                full_searchBox.Checked = true;

            if (row["combined_meBox"].ToString() == "True")
                combined_meBox.Checked = true;

            if (row["field_codingBox"].ToString() == "True")
                field_codingBox.Checked = true;

            if (row["verboseBox"].ToString() == "True")
                verboseBox.Checked = true;

            if (row["localBox"].ToString() == "True")
                localBox.Checked = true;

            startBox1.Text = row["startBox1"].ToString();
            if (row["startBox"].ToString() == "True")
                startBox.Checked = true;

            stopBox1.Text = row["stopBox1"].ToString();
            if (row["stopBox"].ToString() == "True")
                stopBox.Checked = true;

            targetrateBox1.Text = row["targetrateBox1"].ToString();
            if (row["targetrateBox"].ToString() == "True")
                targetrateBox.Checked = true;
       

            
        }

        private void targetrateBox_CheckedChanged(object sender, EventArgs e)
        {
            if (targetrateBox1.Text == "")
                targetrateBox.Checked = false;

            if (targetrateBox.Checked == true)
                targetrate = " -targetrate " + targetrateBox1.Text.ToString();
            else
                targetrate = "";
        }

        private void targetrateBox1_TextChanged(object sender, EventArgs e)
        {
            if (targetrateBox1.Text != "")
                targetrateBox.Checked = true;
            else
                targetrateBox.Checked = false;

            if (targetrateBox.Checked == true)
                targetrate = " -targetrate " + targetrateBox1.Text.ToString();
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Quer realmente deletar " + '"' + UserPresetsBox.Text + '"' + "?", "Delete!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                DataTable xmlConfigUser = GetDataTableSchemaUser();

                //inicio if "se existir preset "name" faça isso"

                var row = (from r in xmlConfigUser.Select()
                           where r["Name"].ToString() == UserPresetsBox.Text
                           select r).FirstOrDefault();


                if (row != null)
                    xmlConfigUser.Rows.Remove(row);
                // fim do if

                xmlConfigUser.WriteXml("presets_user.xml", XmlWriteMode.WriteSchema);
                UserPresetsBox.DataSource = xmlConfigUser;
                UserPresetsBox.DisplayMember = "Name";
                UserPresetsBox.Refresh();
            }
        }

        public void AssociateCallerForm(Form1 form1)
        {
            this.callerForm = form1;
        }

        private void projectNameTextBox_TextChanged(object sender, EventArgs e)
        {
            EnableStartCodec();

            UserPresetsBox.Text = projectNameTextBox.Text;
        }

        private void rwlt_filterBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (rwlt_filterBox1.SelectedIndex > 0)
                rwlt_filterBox.Checked = true;
            else
                rwlt_filterBox.Checked = false;

            if (rwlt_filterBox.Checked == true)
                rwlt_filter = " -rwlt_filter " + rwlt_filterBox1.SelectedItem.ToString();
        }

        private void prefilterBox2_TextChanged(object sender, EventArgs e)
        {
            if (prefilterBox.Checked == true)
                prefilter = " -prefilter " + prefilterBox1.SelectedItem.ToString() + " " + prefilterBox2.Text.ToString();
        }
    }
}
